package com.yycx.module.bus.provider.controller.hy;

import cn.hutool.core.map.MapUtil;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.provider.service.hy.BusHyorderReturnService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * 非两票订单控制器
 *
 * @author flyme
 * @date 2020-11-28
 */
@RestController
@RequestMapping("/hy/order/")
public class HyOrderController {
    @Autowired
    private HyOrderService hyOrderService;

    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private BusHyorderReturnService hyorderReturnService;


    @ApiOperation(value = "非两票订单订单-分页列表", notes = "非两票订单订单分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(HttpServletRequest request, @RequestParam(required = false) Map params) {
        return hyOrderService.pageList(params);
    }

    @ApiOperation(value = "非两票订单订单-列表", notes = "非两票订单订单列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return hyOrderService.listEntityMap(params);
    }

    @ApiOperation(value = "非两票订单订单-添加", notes = "添加非两票订单订单")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        return hyOrderService.add(params);
    }

    @ApiOperation(value = "非两票订单订单-更新", notes = "更新非两票订单订单")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return hyOrderService.edit(params);
    }

    @ApiOperation(value = "非两票订单订单-删除", notes = "删除非两票订单订单")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return hyOrderService.delete(params);
    }


    @ApiOperation(value = "删除退货单", notes = "删除退货单")
    @PostMapping(value = "deleteBackOrder")
    public ResultBody deleteBackOrder(String orderId,String parentId) {
        hyOrderService.deleteBackOrder(orderId,parentId);
        return ResultBody.msg("删除成功");
    }


    @ApiOperation(value = "非两票订单订单-详情", notes = "非两票订单订单详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return hyOrderService.get(params);
    }

    @ApiOperation(value = "从海鹰订单数据库订单初始化", notes = "从海鹰订单数据库订单初始化")
    @PostMapping(value = "syncOrder")
    public ResultBody syncOrder(@RequestParam String orderNo) {
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = openUser.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String dbName = "";
        if (FlymeUtils.isNotEmpty(company)) {
            dbName = company.getBusinessScope();
            ApiAssert.isNotEmpty("账套实例未配置", dbName);
        }
        return hyOrderService.syncOrder(dbName, orderNo);
    }

    @ApiOperation(value = "申请撤销审批", notes = "申请撤销审批")
    @PostMapping(value = "revokeProcess")
    public ResultBody revokeProcess(String orderId) {
        hyOrderService.revokeProcess(orderId);
        return ResultBody.msg("申请成功");
    }


    @ApiOperation(value = "取消申请撤销审批", notes = "取消申请撤销审批")
    @PostMapping(value = "cancelRevokeProcess")
    public ResultBody cancelRevokeProcess(String orderId) {
        hyOrderService.cancelRevokeProcess(orderId);
        return ResultBody.msg("取消成功");
    }


    @ApiOperation(value = "修改订单审核状态", notes = "修改订单审核状态")
    @PostMapping(value = "updateCaclulateOrder")
    public ResultBody updateCaclulateOrder(String orderId) {
        Boolean tag = false;
        if (tag) {
            return ResultBody.msg("核算成功");
        } else {
            return ResultBody.failed("提交失败,存在未核算的订单明细");
        }

    }

    @ApiOperation(value = "退补差价", notes = "同步订单明细到回款表")
    @PostMapping(value = "saveTuibu")
    public ResultBody saveTuibu(String orderId, String drugList) {
        hyOrderService.saveTuibu(orderId, drugList);
        return ResultBody.msg("保存成功");
    }

    @ApiOperation(value = "保存退货申请", notes = "保存退货申请")
    @PostMapping(value = "saveBack")
    public ResultBody saveBack(String orderId,String reason, String returnList) {
        hyOrderService.saveBack(orderId,reason, returnList);
        return ResultBody.msg("保存成功");
    }


    @ApiOperation(value = "同步订单明细到回款表", notes = "同步订单明细到回款表")
    @PostMapping(value = "syncOrderReturn")
    public ResultBody syncOrderReturn(String orderId) {
        hyorderReturnService.syncOrderReturn(orderId);
        return ResultBody.msg("同步成功");
    }


    @ApiOperation(value = "同步退货订单明细到回款表", notes = "同步订单明细到回款表")
    @PostMapping(value = "syncBackOrderReturn")
    public ResultBody syncBackOrderReturn(String orderId) {
        hyorderReturnService.syncBackOrderReturn(orderId);
        return ResultBody.msg("同步成功");
    }

    @ApiOperation(value = "更改发货状态", notes = "更改发货状态")
    @PostMapping(value = "updateSendState")
    public ResultBody updateSendState(String orderId) {
        hyOrderService.updateSendState(orderId, null);
        return ResultBody.msg("更改成功");
    }

    @ApiOperation(value = "获取打印信息", notes = "获取打印信息")
    @GetMapping(value = "getPrintInfo")
    public ResultBody getPrintInfo(String orderId) {
        return hyOrderService.getPrintInfo(orderId);
    }

    @ApiOperation(value = "获取退货单打印信息", notes = "获取退货单打印信息")
    @GetMapping(value = "getBackOrderPrintInfo")
    public ResultBody getBackOrderPrintInfo(String orderId) {
        return hyOrderService.getBackOrderPrintInfo(orderId);
    }


    @PostMapping(value = "updatePrintTag")
    public ResultBody updatePrintTag(@RequestParam String orderId) {
        Boolean tag = hyOrderService.updatePrintTag(orderId);
        return ResultBody.msg("打印成功");
    }

    @PostMapping(value = "updateOutPrintTag")
    public ResultBody updateOutPrintTag(@RequestParam String orderId) {
        hyOrderService.updateOutPrintTag(orderId);
        return ResultBody.msg("打印成功");
    }

    @PostMapping(value = "updateOutPrintTag2")
    public ResultBody updateOutPrintTag2(@RequestParam String orderId) {
        hyOrderService.updateOutPrintTag2(orderId);
        return ResultBody.msg("打印成功");
    }

    @PostMapping(value = "setOutPrintTag")
    public ResultBody setOutPrintTag(@RequestParam String orderId, Integer outPrintTag) {
        hyOrderService.setOutPrintTag(orderId, outPrintTag);
        return ResultBody.msg("设置成功");
    }

    @PostMapping(value = "setOutPrintTag2")
    public ResultBody setOutPrintTag2(@RequestParam String orderId, Integer outPrintTag2) {
        hyOrderService.setOutPrintTag2(orderId, outPrintTag2);
        return ResultBody.msg("设置成功");
    }

    /**
     * 申请开货款发票
     *
     * @param orderId
     * @param invoiceSendType
     * @param invoiceRemark
     * @return
     */
    @PostMapping(value = "updateOrderInvoiceInfo")
    public ResultBody updateOrderInvoiceInfo(@RequestParam String orderId, @RequestParam Integer invoiceSendType, @RequestParam(required = false) String invoiceRemark) {
        Boolean tag = hyOrderService.updateOrderInvoiceInfo(orderId, invoiceSendType, invoiceRemark);
        return ResultBody.ok();
    }

    @ApiOperation(value = "取消申请开货款发票", notes = "取消申请开货款发票")
    @PostMapping(value = "cancelInvoiceInfo")
    public ResultBody cancelInvoiceInfo(String orderId) {
        hyOrderService.cancelInvoiceInfo(orderId);
        return ResultBody.msg("取消成功");
    }

    @ApiOperation(value = "获取出库单打印信息", notes = "获取出库单打印信息")
    @PostMapping(value = "getOutOrderPrint")
    public ResultBody getOutOrderPrint(String orderId) {
        return hyOrderService.getOutOrderPrint(orderId);
    }

    @ApiOperation(value = "获取出库单合并打印信息", notes = "获取出库单合并打印信息")
    @PostMapping(value = "getMergeOutOrderPrint")
    public ResultBody getMergeOutOrderPrint(String orderId) {
        return hyOrderService.getMergeOutOrderPrint(orderId);
    }

    /**
     * 导出数据
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        String companyName = OpenHelper.getAttr("companyName", String.class);
        String handlerName = MapUtil.getStr(params, "handlerName");
        String fileName = MapUtil.getStr(params, "fileName");
        hyOrderService.export(params, request, response, companyName + "-" + fileName, fileName, handlerName);
    }


}
